<?php

/**
 * @copyright   FOSP
 * @version     20120411
 * @todo        Redis access table rating_player
 * @name        Fpt_Business_RatingPlayer_Adapter_Redis
 * @author      QuangTM 
 */
class Fpt_Business_RatingPlayer_Adapter_Redis extends Fpt_Business_RatingPlayer_Adapter_Abstract
{

    const RATING_MATCH = 'rating_match';

    /**
     * Get Fpt_Business_RatingPlayer_Adapter_Redis's instance
     * @return Fpt_Business_RatingPlayer_Adapter_Redis
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Add user rated for player and match
     * @param array $params
     * @return boolean 
     * @author QuangTM
     */
    public function addRatedPlayer($params)
    {
        try
        {
            // Get redis instance
            $redisInstance = Fpt_Global::getRedisInstance(0, $params['MatchID']);

            // Get key redis for adding user to rated match set
            $keyCate = Fpt_Global::makeCacheKey(self::RATING_MATCH, $params['MatchID']);

            // Add user and return result
            return $redisInstance->sAdd($keyCate, $params['UserID']);
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Check user is rated on match
     * @param int $userID
     * @param int $matchID
     * @return boolean 
     * @author QuangTM
     */
    public function isRated($userID, $matchID)
    {
        try
        {
            // Get redis instance
            $redisInstance = Fpt_Global::getRedisInstance(0, $matchID);

            // Get key redis for adding user to rated match set
            $keyCate = Fpt_Global::makeCacheKey(self::RATING_MATCH, $matchID);

            // Check user is rated match
            return $redisInstance->sIsMember($keyCate, $userID);
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}